#include <gtk/gtk.h>
#include <stdio.h>

#include "gtkutils.h"
#include "logs/log.h"
#include "commandutils.h"

void sayHello() {
    printf("hello word\n");
}
#define BREW "brew"
#define BREW_SERVICES   BREW " services"

#define BREW_SERVICES_START     BREW_SERVICES " start %s"
#define BREW_SERVICES_STOP      BREW_SERVICES " stop %s"
#define BREW_SERVICES_RESTART   BREW_SERVICES " restart %s"

#define BREW_STOPPED "stopped"
#define BREW_STARTED "started"

GtkListStore *combo_model;
enum {
    SERVICE_NAME,
    SERVICE_SHOW
};

createCellLayoutDataFunc(renderer_combo_text) {

}

createSignalObjectFunc(click_update, GtkComboBox) {
    listStoreClear(combo_model);
    GtkTreeIter treeIter = {0};

    char buffer[10000] = {0};
    if (getCmdStdoutFull(BREW_SERVICES, buffer, sizeof(buffer))) {
        GSList *lines = split(buffer, '\n');

        for (GSList *iter = g_slist_nth(lines, 1); iter; iter = iter->next) {
            GSList *values = split(iter->data, ' ');
            
            listStoreAppend(combo_model, &treeIter);
            char buffer[100] = {0};
            if (LineHasValue(iter->data, BREW_STARTED)) 
                // listStoreSet(combo_model, &treeIter, SERCICE_STATE, BREW_STARTED, -1);
                // listStoreSet(combo_model, &treeIter, SERVICE_NAME, g_slist_nth(values, 0)->data, -1);
                sprintf(buffer, "%s(%s)",(char*)g_slist_nth(values, 0)->data,BREW_STARTED);
            else
                // listStoreSet(combo_model, &treeIter, SERCICE_STATE, "("BREW_STOPPED")", -1);
                // listStoreSet(combo_model, &treeIter, SERVICE_NAME, g_slist_nth(values, 0)->data, -1);
                sprintf(buffer, "%s(%s)",(char*)g_slist_nth(values, 0)->data,BREW_STOPPED);

            listStoreSet(combo_model, &treeIter, SERVICE_NAME, g_slist_nth(values, 0)->data, SERVICE_SHOW, buffer, -1);

            GS_FREE_ALL_CLEAN(values);
        }
        GS_FREE_ALL_CLEAN(lines);
    }

    setComboBoxActive(user_data, 0);
}

createSignalObjectFunc(click_start, GtkComboBox) {
    GtkTreeIter iter;
    char *str;
    if (gtk_combo_box_get_active_iter(user_data, &iter)) {
        treeModelGet(convTreeModel(combo_model), &iter, SERVICE_NAME, &str, -1);
        DEBUG_LOG(BREW_SERVICES_START, str);

        char command[100] = {0};
        sprintf(command, BREW_SERVICES_START, str);
        system(command);

        S_CLEARN(command);
        sprintf(command, "%s(%s)", str, BREW_STARTED);
        listStoreSet(combo_model, &iter, SERVICE_SHOW, command, -1);
    }
}
createSignalObjectFunc(click_stop, GtkComboBox) {
    GtkTreeIter iter;
    char *str;
    if (gtk_combo_box_get_active_iter(user_data, &iter)) {
        treeModelGet(convTreeModel(combo_model), &iter, SERVICE_NAME, &str, -1);
        DEBUG_LOG(BREW_SERVICES_STOP, str);

        char command[100] = {0};
        sprintf(command, BREW_SERVICES_STOP, str);
        system(command);

        S_CLEARN(command);
        sprintf(command, "%s(%s)", str, BREW_STOPPED);
        listStoreSet(combo_model, &iter, SERVICE_SHOW, command, -1);
    }
}
createSignalObjectFunc(click_restart, GtkComboBox) {
    GtkTreeIter iter;
    char *str;
    if (gtk_combo_box_get_active_iter(user_data, &iter)) {
        treeModelGet(convTreeModel(combo_model), &iter, SERVICE_NAME, &str, -1);
        DEBUG_LOG(BREW_SERVICES_RESTART, str);

        char command[100] = {0};
        sprintf(command, BREW_SERVICES_RESTART, str);
        system(command);
    }
}

static void activate(GtkApplication *app, gpointer user_data) {
    GtkWindow *window;
    window = createWindowWithTitleSize(app, "gtk-brew-services", 200, 100);

    combo_model = createListStore(2, G_TYPE_STRING, G_TYPE_STRING);
    GtkComboBox *combo = createComboBoxWithModel(convTreeModel(combo_model));
    GtkCellRendererText *renderer_text = createCellRendererText();
    // GtkCellRendererText *renderer_state = createCellRendererText();
    setCellLayout(combo, renderer_text, "text", SERVICE_SHOW, renderer_combo_text, false);
    // setCellLayout(combo, renderer_state, "text", SERCICE_STATE, renderer_combo_text, false);

    GtkBox *mainBox = createBoxWithHorizntalAppends(5, 
        createLabel("选择服务:"),
        combo,
        createButtonAction(combo, "刷新", click_update),
        createButtonAction(combo, "启动", click_start),
        createButtonAction(combo, "停止", click_stop),
        createButtonAction(combo, "重启", click_restart),
        NULL
    );
    setWidgetVerticalAlign(mainBox, ASTART);
    setWidgetHorizntalAlign(mainBox, ACENTER);

    setWidgetFullMargin(mainBox, 10);

    setWindowChild(window, mainBox);
    showWindow(window);
}

int	main(int argc, char **argv) {

    GtkApplication *app;
    int status;

    app = createApplicationConnectActivate("gtk.example.template", 
                                G_APPLICATION_FLAGS_NONE, activate);

    status = runApplication(app, argc, argv);
    objectUnref(app);

    return status;
}

